package com.changgou.goods.service.impl;

import com.changgou.core.service.impl.CoreServiceImpl;
import com.changgou.goods.dao.BrandMapper;
import com.changgou.goods.dao.CategoryBrandMapper;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.pojo.CategoryBrand;
import com.changgou.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liyipeng
 * @date 2021-06-26 17:00
 */
@Service
public class BrandServiceImpl extends CoreServiceImpl<Brand> implements BrandService {

    private BrandMapper brandMapper;

    @Autowired
    private CategoryBrandMapper categoryBrandMapper;

    @Autowired
    public BrandServiceImpl(BrandMapper brandMapper) {
        super(brandMapper, Brand.class);
        this.brandMapper = brandMapper;
    }

    @Override
    public List<Brand> findByCategory(Integer categoryId) {

        /*//1.查询当前分类所对应的所有品牌信息
        CategoryBrand categoryBrand = new CategoryBrand();
        categoryBrand.setCategoryId(categoryId);
        List<CategoryBrand> brandIds = categoryBrandMapper.select(categoryBrand);
        //2.根据品牌ID查询对应的品牌集合
        List<Brand> list = new ArrayList<>();
        for (CategoryBrand brandId : brandIds) {
            Brand brand = brandMapper.selectByPrimaryKey(brandId);
            list.add(brand);
        }
        return list;*/

        //上面的方法虽然可以但是麻烦，不如直接创建DAO用SQL语句实现查询
        return brandMapper.findByCategory(categoryId);
    }
}
